package com.quanxiaoha.weblog.web.controller.record;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanxiaoha.weblog.common.aspect.ApiOperationLog;
import com.quanxiaoha.weblog.common.domain.query.CensusQuery;
import com.quanxiaoha.weblog.common.domain.vo.*;
import com.quanxiaoha.weblog.common.domain.entity.ScheduleRecord;
import com.quanxiaoha.weblog.common.domain.query.RecordQuery;
import com.quanxiaoha.weblog.common.domain.query.ScheduleRecordQuery;
import com.quanxiaoha.weblog.common.service.ScheduleRecordService;
import com.quanxiaoha.weblog.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@Api(tags = "日程")
@RequestMapping("/todo")
public class ScheduleRecordController {

    @Autowired
    private ScheduleRecordService scheduleRecordService;

    @PostMapping("/page")
    @ApiOperationLog(description = "获取分页列表")
    @ApiOperation(value = "获取分页列表")
    public Response page(@RequestBody @Validated RecordQuery recordQuery) {

        Page<ScheduleRecordVo> scheduleRecordBoPage = scheduleRecordService.RecordList(recordQuery);
        return Response.success(scheduleRecordBoPage);
    }

    @GetMapping("/list")
    @ApiOperationLog(description = "获取列表")
    @ApiOperation(value = "获取列表")
    public Response list() {

        LambdaQueryWrapper<ScheduleRecord> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(ScheduleRecord::getCreateTime);
        List<ScheduleRecord> list = scheduleRecordService.list(qw);
        return Response.success(list);
    }

    @PostMapping("/reverseList")
    @ApiOperationLog(description = "逆向日程表")
    @ApiOperation(value = "逆向日程表")
    public Response reverseList(@RequestBody @Validated RecordQuery recordQuery) {

        List<ReverseVo> reverseVos = scheduleRecordService.reverseList(recordQuery);
        return Response.success(reverseVos);
    }

    @GetMapping("/detail/{id}")
    @ApiOperationLog(description = "查看详情")
    @ApiOperation(value = "查看详情")
    public Response detail(@PathVariable Integer id) {

        ScheduleRecordVo record = scheduleRecordService.getRecord(id);
        return Response.success(record);
    }

    @PostMapping("/insert")
    @ApiOperationLog(description = "新增记录")
    @ApiOperation(value = "新增记录")
    public Response insert(@RequestBody @Validated ScheduleRecordQuery scheduleRecordQuery) {

        log.info("输入参数："+scheduleRecordQuery);
        scheduleRecordService.insertRecord(scheduleRecordQuery);
        return Response.success();
    }

    @PostMapping("/update")
    @ApiOperationLog(description = "更新记录")
    @ApiOperation(value = "更新记录")
    public Response update(@RequestBody @Validated ScheduleRecordQuery scheduleRecordQuery) {

        scheduleRecordService.updateRecord(scheduleRecordQuery);
        return Response.success();
    }

    @PostMapping("/delete/{id}")
        @ApiOperationLog(description = "删除记录")
    @ApiOperation(value = "删除记录")
    public Response delete(@PathVariable Integer id) {

        scheduleRecordService.deleteRecord(id);
        return Response.success();
    }


    @GetMapping("/tagList")
    @ApiOperationLog(description = "标签列表")
    @ApiOperation(value = "标签列表")
    public Response tagList() {

        List<ScheduleTagVo> scheduleTagVos = scheduleRecordService.tagList();
        return Response.success(scheduleTagVos);
    }

    /**
     * 统计图
     * @return
     */
    @PostMapping("/census")
    @ApiOperationLog(description = "时间统计图")
    @ApiOperation(value = "时间统计图")
    public Response census(@RequestBody @Validated CensusQuery query) {

        List<CensusVo> census = scheduleRecordService.census(query);
        return Response.success(census);
    }


    /**
     * 统计图
     * @return
     */
    @PostMapping("/censusByFeeling")
    @ApiOperationLog(description = "感受统计图")
    @ApiOperation(value = "感受统计图")
    public Response censusByFeeling(@RequestBody @Validated CensusQuery query) {

        List<CensusVo> censusByFeeling = scheduleRecordService.censusByFeeling(query);
        return Response.success(censusByFeeling);
    }
    /**
     * 计算任务
     * @return
     */
    @GetMapping("/calculateTask")
    @ApiOperationLog(description = "计算任务")
    @ApiOperation(value = "计算任务")
    public Response calculateTask(@RequestParam("tagId") Integer tagId) {

        scheduleRecordService.calculateTask(tagId);
        return Response.success();
    }


}
